package sdstcom.lbq.one.lbq.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import org.xutils.x;

import java.util.HashMap;
import java.util.LinkedList;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import sdstcom.lbq.one.R;
import sdstcom.lbq.one.lbq.Databeans.Videobean;

/**
 * Created by Administrator on 2017/1/9 0009.
 */

public class VideoAdapter extends BaseAdapter {
    private Context context;
    private LinkedList<Videobean.DataBean> list;
    private LayoutInflater inflater;
    private HashMap<Integer,Boolean> collectstate;
    public VideoAdapter(Context context, LinkedList<Videobean.DataBean> list) {
        this.context = context;
        this.list = list;
        inflater=LayoutInflater.from(context);
        collectstate=new HashMap<>();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
          final ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.video_list_item, parent, false);
            holder = new ViewHolder(convertView);
            collectstate.put(position,holder.collect.isSelected());
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            if (collectstate.get(position)==null)
            {
                holder.collect.setSelected(false);
                collectstate.put(position,false);
            }
            else
            {
                holder.collect.setSelected(collectstate.get(position));
            }
        }
        holder.title.setText(list.get(position).getTitle());
        holder.subtitle.setText("--关于"+list.get(position).getSubtitle());
        holder.forword.setText(list.get(position).getForward());
        holder.username.setText("文/"+list.get(position).getAuthor().getUser_name());
        x.image().bind(holder.img,list.get(position).getImg_url());
        holder.collect.setTag(list.get(position).getId());
        holder.collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(!collectstate.get(position));
                collectstate.put(position,!collectstate.get(position));

            }
        });
        holder.share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showShare(position);
            }
        });
        return convertView;
    }



    public static class ViewHolder
    {
        private TextView title;
        private TextView subtitle;
        private TextView forword;
        private TextView username;
        private ImageView img;
        private ImageView share;
        private ImageView collect;
        public ViewHolder(View convertView) {
            title= (TextView) convertView.findViewById(R.id.video_item_title);
            subtitle= (TextView) convertView.findViewById(R.id.video_item_subtitle);
            forword= (TextView) convertView.findViewById(R.id.video_item_forward);
            username= (TextView) convertView.findViewById(R.id.video_item_username);
            img= (ImageView) convertView.findViewById(R.id.video_item_img);
            share= (ImageView) convertView.findViewById(R.id.video_item_share);
            collect= (ImageView) convertView.findViewById(R.id.video_item_collcet);
        }
    }
    private void showShare(int position) {
        ShareSDK.initSDK(context.getApplicationContext());
        OnekeyShare oks = new OnekeyShare();
//关闭sso授权
        oks.disableSSOWhenAuthorize();

// title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用
        oks.setTitle(list.get(position).getShare_info().getTitle());
// titleUrl是标题的网络链接，QQ和QQ空间等使用
        oks.setTitleUrl(list.get(position).getShare_info().getUrl());
// text是分享文本，所有平台都需要这个字段
        oks.setText(list.get(position).getShare_info().getContent());
// imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
//oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
// url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
// comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
// site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(context.getString(R.string.app_name));
// siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(context);
    }
}
